<template>
<view class="container">
		<navi-shell :back="true" v-bind:title="courses.length+'门课程'">
			<text class="iconfont icon-sousuo ic-ss" @click="searchKey"></text>
			<text class="iconfont icon-tianjia ic-tj" @tap="clickAddBlog"></text>
		</navi-shell>
		<view v-if="aSearch">
					<uni-search-bar :radius="100" cancelButton="none" placeholder="请输入关键词搜索" class="uni-searchbar" @confirm="search"
						@clear="clearKeywords" />
		</view>
		<view v-if="isLogin" v-for="(course,index) in courses" :key="course.id" class="course-display">
			<image :src="course.cover" mode="aspectFill" class="img-cover" @click.stop="clickShowUser(course)"></image>
			<text class="course-name">{{course.name}}</text>
			<text class="course-nick">{{nick}}</text>
			<text class="course-disc">{{course.disc}}</text>
			<text class="course-Chapter">共{{course.countChapter}}章</text>
			<text class="course-Student">共{{course.countStudent}}人</text>
		</view>
	<dialog-shell ref="shell" title="课程描述" @comfirm="dosth">
				<text class="course-desc">{{courseDisc}}</text>
			</dialog-shell>
		<text class="iconfont icon-xiezuo" @click="clickAddBlog"></text>
	</view>
</template>

<script>
	const app = getApp();
	export default {
		data() {
			return {
				isLogin: false,
				courses: [],
				nick:'',
				courseDisc:'',
				title:'',
				aSearch:false,
				searching: false,
				
			}
		},
		onLoad() {
			this.courseAll()
		},
		mounted() {
			this.isLogin = app.globalData.token.length > 0
			this.nick = app.globalData.nickName
			if (!this.isLogin) {
				delta = 2
				this.$refs.login.show()
			}
		},
		onPullDownRefresh() {
			this.courses = [],
				console.log("执行了下拉刷新")
			this.courseAll()
			uni.stopPullDownRefresh()
		},
		methods: {
			
			courseAll() {
				let url = this.$params.host + this.$params.api_v1 + this.$params.teacher_course + this.$params
					.teacher_teacher
				let data = {
					token: app.globalData.token,
					id: app.globalData.uid
				}
				this.$request.glyAllget(url, data, res => {
					console.log(res)
					res.data.forEach(b => {
						if (!b.cover.startsWith('http')) {
							b.cover = this.$params.host + b.cover
						}
					})
					this.courses = res.data
				});
			},
			clickShowUser(course) {
				this.courseDisc = course.disc
				this.title = course.name
				this.$refs.shell.showDialog()
			},
			dosth() {
							uni.showToast({
								title: "自定义组件事件"
							});
						},
						searchKey(){
										this.aSearch = !this.aSearch
									},
					clearKeywords() {
									this.searching = false
									this.courses = []
									this.courseAll()
								},
								search(e) {
									let kw = e.value
									if (kw.trim().length == 0) {
										return
									}
									this.searching = true
									this.courses = []
									let data = {
										token: app.globalData.token
									}
									let url = this.$params.host+this.$params.api_v1 +this.$params.course+ this.$params.sousuo + kw
									this.$request.glyAllget(url, data, res => {
										res.data.forEach(b => {
											if (!b.cover.startsWith('http')) {
												b.cover = this.$params.host + b.cover
											}
										})
										this.courses = res.data
										console.log(res)
									})
								},
			clickAddBlog(){
				uni.navigateTo({
					url:'../courseTJ/courseTJ',
					success:res =>{},
					fail:() => {},
					complete:() =>{}
				})
			},
		}
	}
</script>

<style>
@import url("courseGl.css")
</style>